package com.js.nickcar.module.addcar.model;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.js.nickcar.R;
import com.js.nickcar.base.BaseHolder;
import com.js.nickcar.entity.remote.CarModelBean;

import java.util.ArrayList;

/**
 * Created by LZY on 2017/10/4.
 */
public class CarModelAdapter extends RecyclerView.Adapter<CarModelAdapter.CarModelHodel> {
    public final Context mContext;
    public final LayoutInflater mInflater;
    private ArrayList<CarModelBean> mModels = new ArrayList<>();

    public CarModelAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext.getApplicationContext());
    }

    @Override
    public CarModelHodel onCreateViewHolder(ViewGroup parent, int viewType) {
        return new CarModelHodel(mInflater.inflate(R.layout.item_car_model, parent, false));
    }

    @Override
    public void onBindViewHolder(CarModelHodel holder, final int position) {
        final CarModelBean item = getItem(position);
        holder.tvCarModel.setText(item.getModel_name());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //选择了车型
                if (onCarModelSelectListener!=null){
                    onCarModelSelectListener.onCarModelSelect(item,position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mModels.size();
    }

    public void setData(ArrayList<CarModelBean> models) {
        mModels.clear();
        mModels.addAll(models);
        notifyDataSetChanged();
    }

    public static class CarModelHodel extends BaseHolder {
        public final TextView tvCarModel;
        public final View viewLine;

        public CarModelHodel(View view) {
            super(view);
            tvCarModel = findViewById(R.id.tv_car_model);
            viewLine = findViewById(R.id.view_line);
        }
    }

    public CarModelBean getItem(int position) {
        return mModels.get(position);
    }


    private onCarModelSelectListener onCarModelSelectListener;

    public void setOnCarModelSelectListener(CarModelAdapter.onCarModelSelectListener onCarModelSelectListener) {
        this.onCarModelSelectListener = onCarModelSelectListener;
    }

    public interface onCarModelSelectListener {
        void onCarModelSelect(CarModelBean item, int position);
    }

}
